package com.jgp.glmeeting.service.impl;

import com.jgp.glmeeting.model.Meeting;
import com.jgp.glmeeting.repository.MeetingRepository;
import com.jgp.glmeeting.service.MeetingManagerRelationService;
import com.jgp.glmeeting.service.MeetingParticipantsRelationService;
import com.jgp.glmeeting.service.MeetingService;
import com.jgp.sys.common.query.OrderList;
import com.jgp.sys.common.query.QueryFilterList;
import com.jgp.sys.ui.Pager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
public class MeetingServiceImpl implements MeetingService {

    @Autowired
    private MeetingRepository meetingRepository;
    @Autowired
    private MeetingParticipantsRelationService meetingParticipantsRelationService;
    @Autowired
    private MeetingManagerRelationService meetingManagerRelationService;

    //添加会议
    @Transactional
    @Override
    public void addMeeting(Meeting m) {
        meetingRepository.createOrUpdate(m);
    }

    //查询会议(分页)
    @Override
    public List<Meeting> queryMeetings(Meeting m, Pager pager) {
        String name=m.getName();
        //倒叙
        OrderList orders = new OrderList();
        orders.addOrder("createDatetime","desc");

        QueryFilterList queryFilterList=new QueryFilterList();
        //以下加限制条件
        if(StringUtils.isNotBlank(name)){
            queryFilterList.addFilter("name","like",name);
        }
        return meetingRepository.read(queryFilterList,orders,pager) ;

    }

    //删除
    @Transactional
    @Override
    public void removeMeeting(String id) {
        meetingRepository.delete(id);
        meetingManagerRelationService.deleteByMeetingId(id);
        meetingParticipantsRelationService.deleteByMeetingId(id);
    }

    //修改根据id查一次会议
    @Override
    public Meeting queryID(String id) {
        return meetingRepository.read(id);
    }

    @Override
    public List<Meeting> queryAll(Meeting m) {
        OrderList orders = new OrderList();
        QueryFilterList queryFilterList=new QueryFilterList();
        orders.addOrder("createDatetime","desc");
        return meetingRepository.read(queryFilterList,orders);
    }

    @Override
    public List<Meeting> queryAllMeeting(List<String> lists) {
//        QueryFilterList filters = new QueryFilterList();
//      filters.addFilter("meetingID",Operator.in,lists);
//        OrderList orders = new OrderList();
//        QueryFilterList queryFilterList=new QueryFilterList();
//        orders.addOrder("createDatetime","desc");


       return meetingRepository.readAll(lists);

//        return  meetingRepository.read(filters,orders);
    }

    @Override
    public List<Meeting> queryListByParticipantsId(String participantsId) {
        if(StringUtils.isBlank(participantsId)){
            return null;
        }
        return meetingRepository.queryListByParticipantsId(participantsId);
    }
}
